package com.wanshi.Test;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

/**
 * @Created by IntelliJ IDEA.
 * @Author Journey
 * @Date 2021/12/17 11:38
 */
public class TestJwt {
    public static void main(String[] args) {
        String token = createJwt();
        System.out.println("生成的令牌："+ token);
        parserJwt(token);
    }

    public static String createJwt () {
        // 当前时间毫秒数
        long now = System.currentTimeMillis();
        //过期时间为10秒
        long exp = now + 1000*10;
        JwtBuilder builder= Jwts.builder().setId("11111")// 是字符串
         .setSubject("admin") // 主题 如用户名
         .setIssuedAt(new Date()) // 签发时间
         .signWith(SignatureAlgorithm.HS256,"mengxuegu");// 签名密钥
         return builder.compact();
    }

    public static void parserJwt(String token) {
        Claims claims = Jwts.parser() .setSigningKey("mengxuegu")// 签名密钥要一致
         .parseClaimsJws(token).getBody();
        System.out.println("id:" + claims.getId());
        System.out.println("subject:" + claims.getSubject());
        // System.out.println("IssuedAt:"+claims.getIssuedAt());

    }

}
